package com.wzdigit.wms.raw.controller.inv;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.request.inv.PrintQrCodeRequest;
import com.wzdigit.wms.basic.domain.dtos.inventory.PrintLabelDto;
import com.wzdigit.wms.basic.domain.dtos.inventory.PrintSnDataDto;
import com.wzdigit.wms.basic.service.code.GoodsSerialCoreService;
import com.wzdigit.wms.basic.service.code.IMailCoreService;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.raw.constant.RawWareConstant;
import com.wzdigit.wms.raw.service.inv.GoodsQrCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 条码标签
 * @author Administrator
 *
 */
@RestController
@Api(tags = "条码标签")
@RequestMapping(value = RawWareConstant.API_VERSION)
public class GoodsQrCodeController {
	
	public static final String REQUEST_MODEL = "/goodsqrcode";

    @Autowired
    private GoodsQrCodeService goodsQrCodeService;
    
    @Autowired
    private GoodsSerialCoreService goodsSerialCoreService;
    
    @Autowired
    private IMailCoreService iMailCoreService;
    
    @ApiOperation(value = "打印标签", notes = "打印标签")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/printQrCode")
    public List<PrintSnDataDto> printQrCode(
    		@RequestBody @Valid PrintQrCodeRequest dto,
            BindingResult bindingResult,
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return goodsQrCodeService.printQrCode(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "获取标签线体批次", notes = "获取标签线体批次")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/getLineAndBatch")
    public PrintLabelDto getLineAndBatch(
            BigDecimal headerId,String skuCode,
            HttpServletResponse servletResponse) {
        try {
            return goodsQrCodeService.getLineAndBatch(headerId,skuCode);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }


    
    @ApiOperation(value = "生成新条码SN", notes = "生成新条码SN")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/generateSn")
    public String generateSn(
    		@ApiParam(value = "条码SN", required = true) 
    		@RequestParam(required = true) String goodsSn,
            HttpServletResponse servletResponse) {
        try {
            return goodsSerialCoreService.generateGoodsSn(goodsSn);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "发送邮件", notes = "发送邮件")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/sendSimpleMail")
    public void sendSimpleMail(
    		@ApiParam(value = "收件人", required = true) 
    		@RequestParam(required = true) String to, 
    		@ApiParam(value = "主题", required = true) 
    		@RequestParam(required = true) String subject, 
    		@ApiParam(value = "内容", required = true) 
    		@RequestParam(required = true) String content,
            HttpServletResponse servletResponse) throws Exception {
        try {
        	iMailCoreService.sendSimpleMail(to, subject, content);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}